<?php 

namespace Admin\Controller;

class SystemController extends BaseController{
    
    public function index(){
       if (IS_POST){
           
           // 获取表单提交的数据
           $data = $this->dataDeal(I('post.'));
           
           foreach ($data as $v){
               $res = M('system')->where(array('name'=>$v['name']))->find();
               if (empty($res)){// 添加数据
                   $b = M('system')->data($v)->add();
               }else {// 修改数据
                   $b = M('system')->where(array('name'=>$v['name']))->save($v);
               }
               if ($b !== false){
                   $success[] = 1;
               }
           }
           
           if (count($success) == count($data)){ // 成功
               $this->ajaxReturn(array('status'=>1,'message'=>'操作成功'));
           }else {// 失败
               $this->ajaxReturn(array('status'=>0,'message'=>'操作失败，请重试！'));
           }
           
       }else {
           
           // 获取数据
           $system = D('System');
           $data = $system->getAllData();
           
           // 传递数据
           $this->assign('data',$data);
           
           $this->display('system');
       }
    }
    
    // 数据处理
    protected function dataDeal($data){
        $res = array();
        foreach ($data as $k=>$v){
            $arr['type'] = $k;
            foreach ($v as $k2=>$s){
                $arr['name'] = $k2;
                $arr['value'] = $s;
                
                $res[] = $arr;
            }
        }
        return $res;
    }
    
}

?>